#include "stdafx.h"

#include "MusicController.h"
#include "InputManager.h"

MusicController::MusicController(Model *model) {
	//Set all starting variables
	m_mainAvatar = NULL;
	setModel(model);
	m_avatarList = new std::list<PlayerAvatar*>();
	m_tensionCurrent = 0;
	m_tensionTarget = 0;
	m_healthCurrent = 10;
	m_healthTarget = 10;
	m_healthMin = 2;
	m_healthMax = 10;
	m_tensionMin = 0;
	m_tensionMax = 10;
	m_healthChangeRate = 0.01f;
	m_tensionChangeRate = 0.005f;

	m_music = new Sound2d(EVENTID_FMODPROJ_MUSIC_ACTION);
	m_eventMusic = false;
	m_musicPlaying = true;

	InputManager::getInstance()->registerObserver(this);
}

MusicController::~MusicController() {
	delete m_avatarList;
	m_avatarList = NULL;
	delete m_music;
	m_music = NULL;
}

void MusicController::startMusic() {
	m_musicPlaying = true;
	SoundPlayer* sp = SoundPlayer::getInstance();
	sp->resetMusicTimer();

	if(m_eventMusic) {
		m_music->play();
	} else {
		sp->activateCue(MUSICCUE_FMODPROJ_START);
		sp->changeMusicParameter(MUSICPARAM_FMODPROJ_HEALTH, m_healthCurrent);
		sp->changeMusicParameter(MUSICPARAM_FMODPROJ_TENSION, m_tensionCurrent);
	}
}
void MusicController::setEventBasedMusic(bool eventBasedMusic) {
	if(eventBasedMusic != m_eventMusic) {
		if(m_musicPlaying) {
			stopMusic();
			m_eventMusic = eventBasedMusic;
			startMusic();
		} else{
			stopMusic();
			m_eventMusic = eventBasedMusic;
		}
	}
}

void MusicController::stopMusic() {
	m_musicPlaying = false;
	m_music->stop();
	SoundPlayer::getInstance()->deactivateCue(MUSICCUE_FMODPROJ_START);
	SoundPlayer::getInstance()->deactivateCue(MUSICCUE_FMODPROJ_ACTION);
	SoundPlayer::getInstance()->deactivateCue(MUSICCUE_FMODPROJ_LOWHEALTH);
	SoundPlayer::getInstance()->resetMusic();	
}

void MusicController::setMainAvatar(PlayerAvatar* playerAvatar) {
	m_mainAvatar = playerAvatar;
}

void MusicController::addAvatar(PlayerAvatar* playerAvatar) {
	m_avatarList->push_back(playerAvatar);
}
void MusicController::removeAvatar(PlayerAvatar* playerAvatar) {
	m_avatarList->remove(playerAvatar);
}
bool MusicController::keyPressed(const OIS::KeyEvent &arg) {
	OIS::Keyboard* keyboard = InputManager::getInstance()->getKeyboard();
   
	if(keyboard->isKeyDown(OIS::KC_F12)) {
  		setEventBasedMusic(!m_eventMusic);
	} else if(keyboard->isKeyDown(OIS::KC_F11)) {
  		stopMusic();
	} else if(keyboard->isKeyDown(OIS::KC_F10)) {
  		startMusic();
	} else if(keyboard->isKeyDown(OIS::KC_F9)) {
  		if(m_mainAvatar) {
			m_mainAvatar->setHealth(100);
			m_mainAvatar->resetFireIntensity();
		}
	}
	
	return true;
}

bool MusicController::update(float deltaTime) {

	SoundPlayer* sp = SoundPlayer::getInstance();
	sp->update(deltaTime);
	if(m_mainAvatar && m_currentModel) {
			
		/**
		*Calculate health parameter
		*/
		float m_healthTarget = m_mainAvatar->getHealth()/10;
		//Keep it in min-max range
		if(m_healthTarget < m_healthMin) {
			m_healthTarget = m_healthMin;
		} else if(m_healthTarget > m_healthMax) {
			m_healthTarget = m_healthMax;
		}
		//move it according to a change rate
		if(fabs(m_healthCurrent - m_healthTarget) < m_healthChangeRate) {
			m_healthCurrent = m_healthTarget;
		}	else if(m_healthCurrent < m_healthTarget) {
			m_healthCurrent += m_healthChangeRate; 
		} else if(m_healthCurrent > m_healthTarget) {
			m_healthCurrent -= m_healthChangeRate; 
		}

		/**
		* Calculate Tension Parameter
		*/
		float m_tensionTarget = m_mainAvatar->getFireIntensity();
		//Keep it in min-max range
		if(m_tensionTarget < m_tensionMin) {
			m_tensionTarget = m_tensionMin;
		} else if(m_tensionTarget > m_tensionMax) {
			m_tensionTarget = m_tensionMax;
		}
		//move it according to a change rate
		if(fabs(m_tensionCurrent - m_tensionTarget)< m_tensionChangeRate) {
			m_tensionCurrent = m_tensionTarget;
		} else if(m_tensionCurrent < m_tensionTarget) {
			m_tensionCurrent += m_tensionChangeRate; 
		} else if(m_tensionCurrent > m_tensionTarget) {
			m_tensionCurrent -= m_tensionChangeRate; 
		}
		/**
		* Send parameters to fmod
		*/
		m_music->changeParam("Health", m_healthCurrent);
		m_music->changeParam("Tension", m_tensionCurrent);
		sp->changeMusicParameter(MUSICPARAM_FMODPROJ_HEALTH, m_healthCurrent);
		sp->changeMusicParameter(MUSICPARAM_FMODPROJ_TENSION, m_tensionCurrent);

		if(!m_eventMusic && m_musicPlaying) {
			//Activate cue each frame
			sp->activateCue(MUSICCUE_FMODPROJ_ACTION);
			sp->activateCue(MUSICCUE_FMODPROJ_LOWHEALTH);
			if(m_mainAvatar->hasJustFragged()) {
				SoundPlayer::getInstance()->activateCue(MUSICCUE_FMODPROJ_FRAG);
			}
		}		
	}
	return true;
}

